//
//  XDBankInfoViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/25.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDBankInfoViewController.h"
#import "AFHTTPSessionManager+POST.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
#import "XDBankInfoModel.h"
#import "MJExtension.h"
#import "XDBankInfoCell.h"
@interface XDBankInfoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *banksArr;

@property (nonatomic,weak) UITableView *tableView;

@end

@implementation XDBankInfoViewController

- (NSArray *)banksArr {
    
    if (_banksArr == nil) {
        _banksArr = [NSArray array];
    }
    return _banksArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"银行卡信息";
    [self setupUI];
    
    [self loadData];
}

- (void)loadData {
    
    NSDictionary * param = @{@"method":@"getBankList"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            self.banksArr = [XDBankInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"banks"]];
            
            [self.tableView reloadData];
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}


- (void)setupUI {
    
    
    UIView *tipBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    tipBackground.backgroundColor = kColorRGB(232,213,177);
    [self.view addSubview:tipBackground];
    
    UILabel *tipLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 30)];
    tipLb.text = @"仅支持借记卡";
    tipLb.textColor = kColorRGB(51,51,51);
    tipLb.font = kBaseFont_(14);
    [tipBackground addSubview:tipLb];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, kScreenW, kScreenH - 64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tableView];
//    self.tableView = tableView;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    tableView.tableFooterView = view;
    self.tableView = tableView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.banksArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"XDBankInfoViewCell";
    XDBankInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        cell = [[XDBankInfoCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:myID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.banksArr[indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XDBankInfoModel *model = self.banksArr[indexPath.row];
    NSString *bankName = model.bankName;
    NSString *bankIconUrl = model.bankIconUrl;
    NSString *uid = [NSString stringWithFormat:@"%zd", model.uid];
    NSDictionary *dic = @{@"bankName":bankName,@"bankIconUrl":bankIconUrl,@"uid":uid,@"bankCode":model.bankCode};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"XDBankInfoCellNoti" object:nil userInfo:dic];

    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}



@end
